<?php

namespace app\admin\controller;

use app\admin\model\AdminModel;
use app\admin\model\ChatModel;

class Chat extends Common
{
    public function getList()
    {
        return view('list');
    }

    public function listData()
    {
        $data = [];
        $page = input('page', 1);
        $limit = input('limit', 10);
        $list = ChatModel::alias('a')->join('tp_admin b', 'a.admin_id = b.id')
            ->field('a.*, b.name')
            ->order('add_time', 'desc')
            ->page($page, $limit)->select();

        $count = ChatModel::alias('a')->join('tp_admin b', 'a.admin_id = b.id')->count();

        foreach ($list as $key => $item){
            $list[$key]['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
        }

        return $this->apiSuccess(['data'=> $list, 'count'=> $count, 'code' => 0]);
    }


    public function add()
    {
        $adminId = input('admin_id', 0);
        $data = [];
        $data['adminList'] = $adminList = AdminModel::where('status', AdminModel::Status1)->where('group_id', '<>', AdminModel::BossGroup)
            ->field('id, name')->select();

        $this->assign('data', $data);
        return view();
    }

    public function edit()
    {
        $adminId = input('admin_id');
        $content = input('content');
        ChatModel::create([
            'admin_id' => $adminId,
            'content' => $content,
            'add_time' => time(),
        ]);
        return $this->apiSuccess('添加成功');
    }

    public function detail()
    {
        $id = input('id');
        $data = ChatModel::find($id);
        $this->assign('data', $data);
        return view();
    }

    public function delete()
    {
        $id = input('id');
        ChatModel::where('id', $id)->delete();
        return $this->apiSuccess('删除成功');
    }
}



